/**
 * Represents a photo (one of the multimedia content types)
 * It's Transferable (used for Drag and Drop operations)
 * 
 * @author remo.lemma@lu.unisi.ch and patrick.zulian@lu.unisi.ch
 */

package ch.unisi.inf.projects.java.uom.elements;

import java.io.File;
import java.io.IOException;
import javax.imageio.ImageIO;
import java.awt.image.BufferedImage;
import java.awt.datatransfer.DataFlavor;
import java.awt.datatransfer.Transferable;
import java.awt.datatransfer.UnsupportedFlavorException;

public class Photo extends Multimedia implements Transferable {
	
	/** THe repreentation of the Photo class as DataFlavor */
	private static final DataFlavor PHOTO_FLAVOR = new DataFlavor(Photo.class, "photo");
	
	/**
	 * Constructor
	 * @param title The title of the photo
	 * @param URL the URL of the photo
	 * @param comment the comment to the photo
	 */
	public Photo (final String title, final String URL, final String comment)
	{
		super(title,URL,comment,"PHOTO");
	}
	
	/**
	 * Constructor (should not be called by the user)
	 * @param id The id of this photo
	 * @param title The title of the photo
	 * @param URL the URL of the photo
	 * @param comment the comment to the photo
	 */
	public Photo (final String id, final String title, final String URL, final String comment)
	{
		super(id, title,URL,comment,"PHOTO");
	}
	
	/**
	 * Returns the image ready to be displayed
	 * @return a Image object representing the image
	 */
	public BufferedImage view()
	{
		try {
			return ImageIO.read(new File(this.getURL()));
		}
		catch (IOException ie)
		{
			return null;
		}
	}
	
	/**
	 * @see java.awt.datatransfer.Transferable#getTransferData(java.awt.datatransfer.DataFlavor)
	 */
	public Object getTransferData (final DataFlavor flavor) throws UnsupportedFlavorException
	{
		if (flavor.getRepresentationClass().isAssignableFrom(Photo.class))
			return view();
		else
			throw new UnsupportedFlavorException(flavor);
	}
	
	/**
	 * @see java.awt.datatransfer.Transferable#getTransferDataFlavors()
	 */
	public DataFlavor[] getTransferDataFlavors ()
	{
		return new DataFlavor[] { PHOTO_FLAVOR };
	}
	
	/**
	 * @see java.awt.datatransfer.Transferable#isDataFlavorSupported(java.awt.datatransfer.DataFlavor)
	 */
	public boolean isDataFlavorSupported (final DataFlavor flavor)
	{
		return flavor.getRepresentationClass().isAssignableFrom(Photo.class);
	}
}
